<!--
 * @Description: ------------ fileDescription -----------
 * @Author: snows_l snows_l@163.com
 * @Date: 2024-08-14 10:00:17
 * @LastEditors: snows_l snows_l@163.com
 * @LastEditTime: 2025-01-17 09:27:52
 * @FilePath: \BLOG\src\views\tools\bgImage\index.vue
-->
<template>
  <div class="image-out-warp">
    <PageTopCover
      :moduleTitle="'精选图库'"
      :textIcon="'🏞️'"
      :coverImg="getOnlineImgUrl('/imgs/bg/57.png')"
      :isMobile="isMobi"
      :mudulDesc="'站长精选背景图，欢迎大家下载收藏！当前图库为压缩过的，如想要更高清的图片 请者联系站长。没有额外做下载入口，直接右键保存即可（移动端长按保存）'"
      :isArticle="false"></PageTopCover>
    <div class="image-container" :class="{ 'm-image-container': isMobi }">
      <div class="center-max-width-warp">
        <div class="image-content-warp">
          <div class="image-layout-tool" v-if="!isMobi" style="margin-right: 10px">
            <div class="img-icon-warp pointer" style="padding: 5px 5px 5px 7px" :class="{ 'icon-active': state.layout === 1 }" @click="handleChangeLayout(1)">
              <img src="@/assets/images/icon/col1.png" fit="cover" />
            </div>
            <div class="img-icon-warp pointer" style="padding: 5px 5px 5px 7px" :class="{ 'icon-active': state.layout === 2 }" @click="handleChangeLayout(2)">
              <img src="@/assets/images/icon/col2.png" />
            </div>
            <div class="img-icon-warp pointer" style="padding: 5px 5px 5px 7px" :class="{ 'icon-active': state.layout === 3 }" @click="handleChangeLayout(3)">
              <img src="@/assets/images/icon/col3.png" />
            </div>
          </div>
          <div class="image-warp-list" v-if="state.list.length > 0">
            <div class="image-item pointer hover-shadow" v-for="(item, index) in state.list" :style="{ width: layoutMap[state.layout].w, height: layoutMap[state.layout].h }">
              <div class="image-item-img intersection-observer">
                <LImg :src="item.url" :preview="state.list.map(item => item.url)" :index="index" />
              </div>
            </div>
          </div>
          <div class="no-article" v-else>
            <Empty :text="'暂无资源，期待您的分享~'" :loadingText="'资源正在拼命加载中...'" :loading="state.loading" />
          </div>
        </div>
        <!-- <LoadingMore
          v-if="store.projectConfig.pageType == 'more'"
          :loading="state.loading"
          :hasMore="state.isMore"
          :length="state.list.length"
          @loadMore="handleLoadMore"></LoadingMore> -->
        <CustomPage :page="state.page" :loading="state.loading" scorllTop="image-container" @change="handlePageChange"></CustomPage>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import useIntersectionObserver from '@/hooks/useIntersectionObserver';
import useResize from '@/hooks/useResize';
import { useAppStore } from '@/store/app';
import { getOnlineImgUrl } from '@/utils/common';
import _ from 'lodash';
import { reactive } from 'vue';

const store = useAppStore();
const { isMobi } = useResize();
useIntersectionObserver(true);
const layoutMap = {
  1: {
    w: '860px',
    h: '400px'
  },
  2: {
    w: '420px',
    h: '240px'
  },
  3: {
    w: '273px',
    h: '160px'
  }
};
const state = reactive({
  page: {
    page: 1,
    size: 6,
    total: 0
  },
  layout: 1,
  list: [],
  listChunk: [],
  loading: false,
  isMore: true
});

let list = [];
for (let i = store.projectConfig.bgImgMaxLen; i >= 1; i--) {
  list.push({
    name: i,
    url: store.projectConfig.assetsPrefix + '/imgs/bg/' + i + '.png',
    loading: false
  });
}

state.page.total = list.length;

state.listChunk = _.chunk(list, state.page.size);
state.list = state.listChunk[state.page.page - 1];

// 加在更多
const handleLoadMore = () => {
  state.loading = true;
  state.page.page++;
  state.list = [...state.list, ...state.listChunk[state.page.page - 1]];
  state.loading = false;
};

// 分页改变
const handlePageChange = (page: number, size: number, scorllTopCallback: Function) => {
  state.page.page = page;
  state.page.size = size;
  state.list = state.listChunk[state.page.page - 1];
  scorllTopCallback && scorllTopCallback();
};

// 布局改变
const handleChangeLayout = (layout: number) => {
  state.page.page = 1;
  state.layout = layout;
  if (layout == 3) {
    state.page.size = 9;
  } else {
    state.page.size = 6;
  }
  state.listChunk = _.chunk(list, state.page.size);
  state.list = state.listChunk[state.page.page - 1];
};
</script>

<style lang="scss" scoped>
.image-out-warp {
  width: 100%;
  height: 100%;
  min-height: 100vh;
  .image-container {
    width: 100%;
    height: 100%;
    min-height: calc(100vh - 400px);
    background-color: var(--bg-image-warp-color);
    padding: 20px 0 0 0;
    .center-max-width-warp {
      height: 100%;
      max-width: var(--content-max-width);
      margin: 0 auto;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      .image-content-warp {
        padding: 0 20px;
        border-radius: var(--border-radius-2);
        .image-layout-tool {
          margin-top: 10px;
          display: flex;
          justify-content: flex-end;
          .img-icon-warp {
            border-radius: var(--border-radius-1);
            border: 1px solid var(--border-color);
            width: 30px;
            height: 30px;
            margin-left: 10px;
            display: flex;
            justify-content: center;
            align-items: center;
            img {
              height: 100%;
            }
          }
          .icon-active {
            border-width: 2px;
            border-color: var(--theme-color);
          }
        }
        .image-warp-list {
          display: flex;
          flex-wrap: wrap;
          .image-item {
            border-radius: var(--border-radius-2);
            margin: 10px 10px;
            display: flex;
            flex-direction: column;
            align-items: center;
            overflow: hidden;
            .image-item-img {
              width: 100%;
              height: 100%;
              overflow: hidden;
              transition: all 1.2s ease-in-out;
              box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
              position: relative;
              img {
                width: 100%;
                height: 100%;
                object-fit: cover;
                border-radius: var(--border-radius-1);
              }
              .download-btn-warp {
                transition: opacity 0.5s;
                opacity: 0;
                position: absolute;
                top: 15px;
                right: 15px;
                .download-btn {
                  border: 1px solid var(--border-color);
                  padding: 6px 10px;
                  border-radius: var(--border-radius-1);
                  color: var(--border-color);
                }
              }
              &:hover {
                .download-btn-warp {
                  opacity: 1;
                }
              }
            }
          }
        }
      }
    }
  }
  .m-image-container {
    min-height: calc(100vh - 300px);
    .center-max-width-warp {
      width: var(--content-max-width-m) !important;
      .image-item {
        width: 100% !important;
        height: 48vw !important;
        margin: 10px 0 !important;
      }
    }
  }
}

.pagination-warpper {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 20px;
  .pagination-warp {
    display: flex;
  }
  .m-page,
  .btn {
    width: 30px;
    height: 30px;
    border-radius: var(--border-radius-2);
    margin: 0 5px;
    background-color: var(--bg-content-color);
    border: 1px solid var(--border-color);
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .active {
    // background-color: var(--theme-light-color-2);
    color: var(--theme-color);
    border-color: var(--theme-color);
  }
  .img-btn {
    width: 20px;
    height: 20px;
    margin: 0 15px;
  }
  .m-page {
    margin: 0 10px;
  }
}
</style>
